﻿using Microsoft.Extensions.DependencyInjection;
using Quartz;
using Quartz.AspNetCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuartzJob.JobWorkAddService
{
    public static class JobWorkAddService
    {
        //注入所有定时任务,后续新加定时任务需要这里配置
        public static void AddJobWorkServiceInject(this IServiceCollection service)
        {
            //注入定时任务
            service.AddTransient<ICeshiJob>();
            service.AddTransient<ICeshiTwoJob>();

            service.AddQuartz(q =>
            {
                q.ScheduleJob<ICeshiJob>(trigger => trigger
               .WithIdentity("myFirstJobTrigger")
               .StartNow()
               .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()));


                q.ScheduleJob<ICeshiTwoJob>(trigger => trigger
                .WithIdentity("mySecondJobTrigger")
                .StartNow()
                .WithCronSchedule("*/20 * * * * ?"));
            });
            // ASP.NET核心托管-添加Quartz服务
            service.AddQuartzServer(options =>
            {
                // 关闭时，我们希望作业正常完成
                options.WaitForJobsToComplete = true;
            });
        }
    }
}
